{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn import preprocessing\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import time\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import interp\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "from sklearn.ensemble import ExtraTreesClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn import svm\n",
    "from collections import Counter\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import roc_auc_score, auc\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(directory):\n",
    "\n",
    "    D_SSM = np.loadtxt(directory + '/D_SM.txt')\n",
    "    M_FSM = np.loadtxt(directory + '/M_SM.txt')\n",
    "    print(\"D_SSM\",D_SSM.shape)\n",
    "    print(\"M_FSM\",M_FSM.shape)\n",
    "\n",
    "    #D_SSM = np.loadtxt(directory + '/D_SM.txt')\n",
    "\n",
    "\n",
    "\n",
    "    ID = np.zeros(shape=(D_SSM.shape[0], D_SSM.shape[1]))\n",
    "    IM = np.zeros(shape=(M_FSM.shape[0], M_FSM.shape[1]))\n",
    "\n",
    "    for i in range(D_SSM.shape[0]):\n",
    "        for j in range(D_SSM.shape[1]):\n",
    "            if D_SSM[i][j] == 0:\n",
    "                ID[i][j] = D_SSM[i][j]######D_GSM[i][j]\n",
    "            else:\n",
    "                ID[i][j] = D_SSM[i][j]\n",
    "    for i in range(M_FSM.shape[0]):\n",
    "        for j in range(M_FSM.shape[1]):\n",
    "            if M_FSM[i][j] == 0:\n",
    "                IM[i][j] = M_FSM[i][j]#######M_GSM[i][j]\n",
    "            else:\n",
    "                IM[i][j] = M_FSM[i][j]\n",
    "                \n",
    "    ID = pd.DataFrame(ID).reset_index()\n",
    "    IM = pd.DataFrame(IM).reset_index()\n",
    "    ID.rename(columns = {'index':'id'}, inplace = True)\n",
    "    IM.rename(columns = {'index':'id'}, inplace = True)\n",
    "    ID['id'] = ID['id'] + 1\n",
    "    IM['id'] = IM['id'] + 1\n",
    "    \n",
    "    return ID, IM\n",
    "\n",
    "def sample(directory, random_seed):\n",
    "    all_associations = pd.read_csv(directory + '/drug_mutation_pairs.csv', names=['Drug', 'Mutation', 'label'])\n",
    "    known_associations = all_associations.loc[all_associations['label'] == 1]\n",
    "    known_associations_resistance=all_associations.loc[all_associations['label'] == -1]\n",
    "    unknown_associations = all_associations.loc[all_associations['label'] == 0]\n",
    "    #random_negative = unknown_associations.sample(n=known_associations.shape[0], random_state=random_seed, axis=0)\n",
    "    random_negative = unknown_associations.sample(n=known_associations.shape[0]+known_associations_resistance.shape[0], random_state=random_seed, axis=0)\n",
    "\n",
    "    sample_df = known_associations.append(random_negative)\n",
    "    sample_df.reset_index(drop=True, inplace=True)\n",
    "\n",
    "    return sample_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def performances(y_true, y_pred, y_prob):\n",
    "\n",
    "    print('y_true:',y_true)\n",
    "    print('y_pred:',y_pred)\n",
    "    #tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels = [0, 1]).ravel().tolist()\n",
    "    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel().tolist()\n",
    "\n",
    "    pos_acc = tp / sum(y_true)\n",
    "    neg_acc = tn / (len(y_pred) - sum(y_pred)) # [y_true=0 & y_pred=0] / y_pred=0\n",
    "    accuracy = (tp+tn)/(tn+fp+fn+tp)\n",
    "    \n",
    "    recall = tp / (tp+fn)\n",
    "    precision = tp / (tp+fp)\n",
    "    f1 = 2*precision*recall / (precision+recall)\n",
    "    \n",
    "    roc_auc = roc_auc_score(y_true, y_prob)\n",
    "    prec, reca, _ = precision_recall_curve(y_true, y_prob)\n",
    "    aupr = auc(reca, prec)\n",
    "    \n",
    "    print('tn = {}, fp = {}, fn = {}, tp = {}'.format(tn, fp, fn, tp))\n",
    "    print('y_pred: -1 = {} |0 = {} | 1 = {}'.format(Counter(y_pred)[0], Counter(y_pred)[1],Counter(y_pred)[2]))###\n",
    "    print('y_true: -1 = {} | 0 = {} | 1 = {}'.format(Counter(y_true)[0], Counter(y_true)[1], Counter(y_true)[2]))###\n",
    "    print('acc={:.4f}|precision={:.4f}|recall={:.4f}|f1={:.4f}|auc={:.4f}|aupr={:.4f}|pos_acc={:.4f}|neg_acc={:.4f}'.format(accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc))\n",
    "    return (y_true, y_pred, y_prob), (accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obtain_data(directory, isbalance):\n",
    "\n",
    "    ID, IM = load_data(directory)\n",
    "\n",
    "    if isbalance:\n",
    "        dtp = sample(directory, random_seed = 1234)\n",
    "    else:\n",
    "        dtp = pd.read_csv(directory + '/drug_mutation_pairs.csv', names=['Drug', 'Mutation', 'label'])\n",
    "\n",
    "    mirna_ids = list(set(dtp['Drug']))\n",
    "    disease_ids = list(set(dtp['Mutation']))\n",
    "    random.shuffle(mirna_ids)\n",
    "    random.shuffle(disease_ids)\n",
    "    print('# Drug = {} | Mutation = {}'.format(len(mirna_ids), len(disease_ids)))\n",
    "\n",
    "    mirna_test_num = int(len(mirna_ids) / 5)\n",
    "    disease_test_num = int(len(disease_ids) / 5)\n",
    "    print('# Test: Drug = {} | Mutation = {}'.format(mirna_test_num, disease_test_num))  \n",
    "    print(dtp)\n",
    "    print(\"ID\",ID)\n",
    "    print(\"IM\",IM)\n",
    "    print(dtp.shape)\n",
    "    print(\"ID\",ID.shape)\n",
    "    print(\"IM\",IM.shape)    \n",
    "    \n",
    "    samples = pd.merge(pd.merge(dtp, ID, left_on = 'Drug', right_on = 'id'), IM, left_on = 'Mutation', right_on = 'id')\n",
    "    samples.drop(labels = ['id_x', 'id_y'], axis = 1, inplace = True)\n",
    "    \n",
    "    return ID, IM, dtp, mirna_ids, disease_ids, mirna_test_num, disease_test_num, samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tp_train_test_idx(samples):\n",
    "    kf = KFold(n_splits = 5, shuffle = True, random_state = 1234)\n",
    "\n",
    "    train_index_all, test_index_all, n = [], [], 0\n",
    "    train_id_all, test_id_all = [], []\n",
    "    fold = 0\n",
    "    for train_idx, test_idx in tqdm(kf.split(samples.iloc[:, 3:])): #train_index与test_index为下标\n",
    "        print('-------Fold ', fold)\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "\n",
    "        train_id_all.append(np.array(dtp.iloc[train_idx][['Drug', 'Mutation']]))\n",
    "        test_id_all.append(np.array(dtp.iloc[test_idx][['Drug', 'Mutation']]))\n",
    "\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        fold += 1\n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tm_Td_train_test_idx(item, ids, dtp):\n",
    "    \n",
    "    test_num = int(len(ids) / 5)\n",
    "    \n",
    "    train_index_all, test_index_all = [], []\n",
    "    train_id_all, test_id_all = [], []\n",
    "    \n",
    "    for fold in range(5):\n",
    "        print('-------Fold ', fold)\n",
    "        if fold != 4:\n",
    "            test_ids = ids[fold * test_num : (fold + 1) * test_num]\n",
    "        else:\n",
    "            test_ids = ids[fold * test_num :]\n",
    "\n",
    "        train_ids = list(set(ids) ^ set(test_ids))\n",
    "        print('# {}: Train = {} | Test = {}'.format(item, len(train_ids), len(test_ids)))\n",
    "\n",
    "        test_idx = dtp[dtp[item].isin(test_ids)].index.tolist()\n",
    "        train_idx = dtp[dtp[item].isin(train_ids)].index.tolist()\n",
    "        random.shuffle(test_idx)\n",
    "        random.shuffle(train_idx)\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        assert len(train_idx) + len(test_idx) == len(dtp)\n",
    "\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "        \n",
    "        train_id_all.append(train_ids)\n",
    "        test_id_all.append(test_ids)\n",
    "        \n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_rf(train_index_all, test_index_all, samples, classfier):\n",
    "    \n",
    "    fold = 0\n",
    "    for train_idx, test_idx in zip(train_index_all, test_index_all):\n",
    "        print('-----------------------Fold = ', str(fold))\n",
    "\n",
    "        X = samples.iloc[:, 3:]\n",
    "        y = samples['label']\n",
    "\n",
    "        scaler = preprocessing.MinMaxScaler().fit(X.iloc[train_idx,:])\n",
    "        X = scaler.transform(X)\n",
    "\n",
    "        x_train, y_train = X[train_idx], y[train_idx]\n",
    "        x_test, y_test = X[test_idx], y[test_idx]\n",
    "\n",
    "        if classfier == 'ERT':\n",
    "            clf = ExtraTreesClassifier(random_state = 19961231)\n",
    "        elif classfier == 'GNB':\n",
    "            clf = GaussianNB()\n",
    "        elif classfier == 'DT':\n",
    "            clf = DecisionTreeClassifier(random_state = 19961231)\n",
    "        elif classfier == 'SGD':\n",
    "            clf = SGDClassifier(loss=\"hinge\", penalty=\"l2\", max_iter=5)\n",
    "        elif classfier == 'SVM':\n",
    "            clf = svm.SVC()\n",
    "            \n",
    "        clf.fit(x_train, y_train)\n",
    "\n",
    "        y_train_prob = clf.predict_proba(x_train)\n",
    "        y_test_prob = clf.predict_proba(x_test)\n",
    "\n",
    "        y_train_pred = clf.predict(x_train)\n",
    "        y_test_pred = clf.predict(x_test)\n",
    "\n",
    "        print('Train:')\n",
    "        ys_train, metrics_train = performances(y_train, y_train_pred, y_train_prob[:, 1])\n",
    "        print('Test:')\n",
    "        ys_test, metrics_test = performances(y_test, y_test_pred, y_test_prob[:, 1])\n",
    "\n",
    "        fold += 1\n",
    "    \n",
    "    return ys_train, metrics_train, ys_test, metrics_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D_SSM (184, 184)\n",
      "M_FSM (661, 661)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 385.65it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Drug = 184 | Mutation = 640\n",
      "# Test: Drug = 36 | Mutation = 128\n",
      "      Drug  Mutation  label\n",
      "0        1       154      1\n",
      "1        2       528      1\n",
      "2        3       451      1\n",
      "3        5       277      1\n",
      "4        5       564      1\n",
      "...    ...       ...    ...\n",
      "2584     8       252      0\n",
      "2585    32        31      0\n",
      "2586   180       656      0\n",
      "2587    20        81      0\n",
      "2588   101       191      0\n",
      "\n",
      "[2589 rows x 3 columns]\n",
      "ID       id         0         1         2         3         4         5  \\\n",
      "0      1  1.000000  0.897662  0.969539  0.977718  0.895714  0.955645   \n",
      "1      2  0.897662  1.000000  0.973867  0.967525  0.998237  0.974889   \n",
      "2      3  0.969539  0.973867  1.000000  0.997122  0.973182  0.986712   \n",
      "3      4  0.977718  0.967525  0.997122  1.000000  0.964158  0.991381   \n",
      "4      5  0.895714  0.998237  0.973182  0.964158  1.000000  0.966592   \n",
      "..   ...       ...       ...       ...       ...       ...       ...   \n",
      "179  180  0.862918  0.675884  0.775158  0.802688  0.655391  0.796309   \n",
      "180  181  0.973597  0.972996  0.994931  0.997336  0.970741  0.991165   \n",
      "181  182  0.938368  0.991783  0.989274  0.984975  0.992872  0.980042   \n",
      "182  183  0.945639  0.988524  0.995966  0.991062  0.987236  0.986661   \n",
      "183  184  0.932862  0.981167  0.987583  0.978269  0.986323  0.962156   \n",
      "\n",
      "            6         7         8  ...       174       175       176  \\\n",
      "0    0.946404  0.989230  0.885793  ...  0.981128  0.981633  0.938121   \n",
      "1    0.990946  0.900367  0.992380  ...  0.892665  0.895908  0.813548   \n",
      "2    0.994273  0.963344  0.967592  ...  0.966367  0.959415  0.885173   \n",
      "3    0.990337  0.977575  0.954821  ...  0.972319  0.973149  0.905176   \n",
      "4    0.990725  0.890795  0.997502  ...  0.887712  0.885108  0.801731   \n",
      "..        ...       ...       ...  ...       ...       ...       ...   \n",
      "179  0.738956  0.892354  0.626119  ...  0.847433  0.868224  0.956633   \n",
      "180  0.992870  0.970224  0.961541  ...  0.956411  0.962710  0.902469   \n",
      "181  0.998560  0.931316  0.989023  ...  0.925891  0.923723  0.857679   \n",
      "182  0.997835  0.944274  0.981751  ...  0.947118  0.939886  0.864105   \n",
      "183  0.991868  0.916459  0.988846  ...  0.931578  0.912692  0.831499   \n",
      "\n",
      "          177       178       179       180       181       182       183  \n",
      "0    0.923775  0.985842  0.862918  0.973597  0.938368  0.945639  0.932862  \n",
      "1    0.790636  0.883018  0.675884  0.972996  0.991783  0.988524  0.981167  \n",
      "2    0.864696  0.961228  0.775158  0.994931  0.989274  0.995966  0.987583  \n",
      "3    0.891424  0.969715  0.802688  0.997336  0.984975  0.991062  0.978269  \n",
      "4    0.768920  0.877347  0.655391  0.970741  0.992872  0.987236  0.986323  \n",
      "..        ...       ...       ...       ...       ...       ...       ...  \n",
      "179  0.942702  0.858934  1.000000  0.791763  0.727605  0.745174  0.696908  \n",
      "180  0.883940  0.955488  0.791763  1.000000  0.989882  0.989944  0.979530  \n",
      "181  0.820336  0.919242  0.727605  0.989882  1.000000  0.994660  0.992953  \n",
      "182  0.839641  0.938761  0.745174  0.989944  0.994660  1.000000  0.991536  \n",
      "183  0.782923  0.922494  0.696908  0.979530  0.992953  0.991536  1.000000  \n",
      "\n",
      "[184 rows x 185 columns]\n",
      "IM       id         0         1         2         3         4         5  \\\n",
      "0      1  1.000000  0.907669  0.176090  0.702117  0.286608  0.490720   \n",
      "1      2  0.907669  1.000000  0.018354  0.921630  0.071288  0.220028   \n",
      "2      3  0.176090  0.018354  1.000000  0.022731  0.981050  0.879705   \n",
      "3      4  0.702117  0.921630  0.022731  1.000000  0.001701  0.042796   \n",
      "4      5  0.286608  0.071288  0.981050  0.001701  1.000000  0.952651   \n",
      "..   ...       ...       ...       ...       ...       ...       ...   \n",
      "656  657  0.399699  0.146881  0.933354  0.012895  0.983124  0.988453   \n",
      "657  658  0.170419  0.045495  0.956196  0.078107  0.926840  0.814243   \n",
      "658  659  0.895244  0.975839  0.045806  0.894017  0.099658  0.246381   \n",
      "659  660  0.016117  0.080791  0.834500  0.285888  0.722721  0.516941   \n",
      "660  661  0.031461  0.079063  0.845736  0.264039  0.740647  0.546501   \n",
      "\n",
      "            6         7         8  ...       651       652       653  \\\n",
      "0    0.719039  0.970476  0.852814  ...  0.018562  0.262593  0.080250   \n",
      "1    0.444454  0.974827  0.867932  ...  0.049907  0.506563  0.238188   \n",
      "2    0.684846  0.083677  0.195105  ...  0.876980  0.370412  0.650142   \n",
      "3    0.188250  0.817615  0.711285  ...  0.234733  0.753886  0.483892   \n",
      "4    0.803060  0.173261  0.276134  ...  0.775504  0.257325  0.525321   \n",
      "..        ...       ...       ...  ...       ...       ...       ...   \n",
      "656  0.888592  0.274203  0.372869  ...  0.668887  0.180138  0.424284   \n",
      "657  0.622768  0.091308  0.197611  ...  0.891163  0.500563  0.749689   \n",
      "658  0.461533  0.956461  0.852942  ...  0.076113  0.548616  0.291475   \n",
      "659  0.282391  0.018094  0.093945  ...  0.995184  0.753412  0.942234   \n",
      "660  0.314889  0.025820  0.105386  ...  0.987390  0.741837  0.940644   \n",
      "\n",
      "          654       655       656       657       658       659       660  \n",
      "0    0.547275  0.022780  0.399699  0.170419  0.895244  0.016117  0.031461  \n",
      "1    0.808724  0.074212  0.146881  0.045495  0.975839  0.080791  0.079063  \n",
      "2    0.098734  0.846843  0.933354  0.956196  0.045806  0.834500  0.845736  \n",
      "3    0.970415  0.269916  0.012895  0.078107  0.894017  0.285888  0.264039  \n",
      "4    0.035068  0.740986  0.983124  0.926840  0.099658  0.722721  0.740647  \n",
      "..        ...       ...       ...       ...       ...       ...       ...  \n",
      "656  0.008320  0.635777  1.000000  0.883281  0.181613  0.613177  0.642788  \n",
      "657  0.178040  0.889241  0.883281  1.000000  0.126786  0.859227  0.887914  \n",
      "658  0.794568  0.118486  0.181613  0.126786  1.000000  0.107377  0.124599  \n",
      "659  0.449486  0.995092  0.613177  0.859227  0.107377  1.000000  0.993158  \n",
      "660  0.424641  0.993029  0.642788  0.887914  0.124599  0.993158  1.000000  \n",
      "\n",
      "[661 rows x 662 columns]\n",
      "(2589, 3)\n",
      "ID (184, 185)\n",
      "IM (661, 662)\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 2071 | Test = 518\n",
      "-------Fold  1\n",
      "# Pairs: Train = 2071 | Test = 518\n",
      "-------Fold  2\n",
      "# Pairs: Train = 2071 | Test = 518\n",
      "-------Fold  3\n",
      "# Pairs: Train = 2071 | Test = 518\n",
      "-------Fold  4\n",
      "# Pairs: Train = 2072 | Test = 517\n",
      "-----------------------Fold =  0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-62-cff714585824>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     23\u001b[0m             \u001b[0mtrain_index_all\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_index_all\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_id_all\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_id_all\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgenerate_task_Tm_Td_train_test_idx\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mids\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     24\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 25\u001b[1;33m         \u001b[0mys_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmetrics_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mys_test\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmetrics_test\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrun_rf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrain_index_all\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_index_all\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msamples\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ERT'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-61-fb6435490eb1>\u001b[0m in \u001b[0;36mrun_rf\u001b[1;34m(train_index_all, test_index_all, samples, classfier)\u001b[0m\n\u001b[0;32m     25\u001b[0m             \u001b[0mclf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msvm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSVC\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m         \u001b[0mclf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m         \u001b[0my_train_prob\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mclf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict_proba\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\sklearn\\ensemble\\_forest.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight)\u001b[0m\n\u001b[0;32m    391\u001b[0m                     \u001b[0mverbose\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mverbose\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mclass_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclass_weight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    392\u001b[0m                     n_samples_bootstrap=n_samples_bootstrap)\n\u001b[1;32m--> 393\u001b[1;33m                 for i, t in enumerate(trees))\n\u001b[0m\u001b[0;32m    394\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    395\u001b[0m             \u001b[1;31m# Collect newly grown trees\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, iterable)\u001b[0m\n\u001b[0;32m   1042\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_iterating\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_original_iterator\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1043\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1044\u001b[1;33m             \u001b[1;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdispatch_one_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1045\u001b[0m                 \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1046\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36mdispatch_one_batch\u001b[1;34m(self, iterator)\u001b[0m\n\u001b[0;32m    857\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    858\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 859\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_dispatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtasks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    860\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    861\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m_dispatch\u001b[1;34m(self, batch)\u001b[0m\n\u001b[0;32m    775\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    776\u001b[0m             \u001b[0mjob_idx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 777\u001b[1;33m             \u001b[0mjob\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    778\u001b[0m             \u001b[1;31m# A job can complete so quickly than its callback is\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    779\u001b[0m             \u001b[1;31m# called before we get here, causing self._jobs to\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\_parallel_backends.py\u001b[0m in \u001b[0;36mapply_async\u001b[1;34m(self, func, callback)\u001b[0m\n\u001b[0;32m    206\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mapply_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    207\u001b[0m         \u001b[1;34m\"\"\"Schedule a func to be run\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 208\u001b[1;33m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mImmediateResult\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    209\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    210\u001b[0m             \u001b[0mcallback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\_parallel_backends.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, batch)\u001b[0m\n\u001b[0;32m    570\u001b[0m         \u001b[1;31m# Don't delay the application, to avoid keeping the input\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    571\u001b[0m         \u001b[1;31m# arguments in memory\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 572\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    573\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    574\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    261\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mparallel_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_jobs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_n_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    262\u001b[0m             return [func(*args, **kwargs)\n\u001b[1;32m--> 263\u001b[1;33m                     for func, args, kwargs in self.items]\n\u001b[0m\u001b[0;32m    264\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    265\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__reduce__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\joblib\\parallel.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    261\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mparallel_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_jobs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_n_jobs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    262\u001b[0m             return [func(*args, **kwargs)\n\u001b[1;32m--> 263\u001b[1;33m                     for func, args, kwargs in self.items]\n\u001b[0m\u001b[0;32m    264\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    265\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__reduce__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\sklearn\\utils\\fixes.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m    220\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    221\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mconfig_context\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 222\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\sklearn\\ensemble\\_forest.py\u001b[0m in \u001b[0;36m_parallel_build_trees\u001b[1;34m(tree, forest, X, y, sample_weight, tree_idx, n_trees, verbose, class_weight, n_samples_bootstrap)\u001b[0m\n\u001b[0;32m    169\u001b[0m         \u001b[0mtree\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcurr_sample_weight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcheck_input\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    170\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 171\u001b[1;33m         \u001b[0mtree\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcheck_input\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    172\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    173\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mtree\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\sklearn\\tree\\_classes.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, check_input, X_idx_sorted)\u001b[0m\n\u001b[0;32m    900\u001b[0m             \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    901\u001b[0m             \u001b[0mcheck_input\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcheck_input\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 902\u001b[1;33m             X_idx_sorted=X_idx_sorted)\n\u001b[0m\u001b[0;32m    903\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    904\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda3\\envs\\MDA-GCNGS\\lib\\site-packages\\sklearn\\tree\\_classes.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, check_input, X_idx_sorted)\u001b[0m\n\u001b[0;32m    387\u001b[0m                                            min_impurity_split)\n\u001b[0;32m    388\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 389\u001b[1;33m         \u001b[0mbuilder\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtree_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    390\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    391\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mn_outputs_\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mis_classifier\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "directory='C:/Users/xs/Desktop/图采样data/last data/'\n",
    "#directory = 'data'\n",
    "for isbalance in [True]:\n",
    "#for isbalance in [True, False]:\n",
    "    \n",
    "    ID, IM, dtp, mirna_ids, disease_ids, mirna_test_num, disease_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp', 'Tm', 'Td']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tm':\n",
    "            item = 'Drug'\n",
    "            ids = mirna_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tm_Td_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Td':\n",
    "            item = 'Mutation'\n",
    "            ids = disease_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tm_Td_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_rf(train_index_all, test_index_all, samples, 'ERT')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
